<html>
<head><meta charset="utf-8"><title>Why can fn ptrs not be used as outputs? · project-inline-asm · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/index.html">project-inline-asm</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Why.20can.20fn.20ptrs.20not.20be.20used.20as.20outputs.3F.html">Why can fn ptrs not be used as outputs?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="245723743"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Why%20can%20fn%20ptrs%20not%20be%20used%20as%20outputs%3F/near/245723743" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Adam Gemmell <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Why.20can.20fn.20ptrs.20not.20be.20used.20as.20outputs.3F.html#245723743">(Jul 12 2021 at 18:02)</a>:</h4>
<p>Hello, I've been reading through the inline-assembly tests and noticed <a href="https://github.com/rust-lang/rust/blob/3a24abd22fd25c836d8b4d75ff46c833f9c3934c/src/test/ui/asm/type-check-2.rs#L72">this</a> line. Other experiments narrowed the failure case down to a function pointer having been used for an output. </p>
<p>The <a href="https://doc.rust-lang.org/beta/unstable-book/library-features/asm.html#register-operands">book</a> doesn't seem to define this, and the error <a href="https://github.com/rust-lang/rust/blob/3a24abd22fd25c836d8b4d75ff46c833f9c3934c/compiler/rustc_passes/src/intrinsicck.rs#L152">source</a> doesn't make it clear either. Could someone clarify please?</p>



<a name="245770761"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Why%20can%20fn%20ptrs%20not%20be%20used%20as%20outputs%3F/near/245770761" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> asquared31415 <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Why.20can.20fn.20ptrs.20not.20be.20used.20as.20outputs.3F.html#245770761">(Jul 13 2021 at 01:44)</a>:</h4>
<p>The type of the value that you get when you refer to a function by name isn't quite a normal fn ptr.  The error you get actually does say that fn ptrs work, and code that does something like</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">foo</span>: <span class="nc">fn</span><span class="p">()</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bar</span><span class="p">;</span><span class="w"></span>
</code></pre></div>
<p>can use <code>foo</code> in that position (assuming <code>bar</code> exists and has the right signature to coerce to the fn ptr you specify)</p>
<p>Example (this code is very wrong logically, it needs clobbers and such, but this is just to get it to compile): <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=f9e4a176715d769338fdc488e7f21d71">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=f9e4a176715d769338fdc488e7f21d71</a></p>



<a name="245770942"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Why%20can%20fn%20ptrs%20not%20be%20used%20as%20outputs%3F/near/245770942" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> asquared31415 <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Why.20can.20fn.20ptrs.20not.20be.20used.20as.20outputs.3F.html#245770942">(Jul 13 2021 at 01:48)</a>:</h4>
<p>(related: can we make this work by doing the coercion automatically?  That would be really nice for users)</p>



<a name="245791102"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Why%20can%20fn%20ptrs%20not%20be%20used%20as%20outputs%3F/near/245791102" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Why.20can.20fn.20ptrs.20not.20be.20used.20as.20outputs.3F.html#245791102">(Jul 13 2021 at 08:11)</a>:</h4>
<p>The coercion already exists, but only for <code>in</code>. It doesn't make sense for <code>out</code>/<code>inout</code>.</p>



<a name="245794512"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Why%20can%20fn%20ptrs%20not%20be%20used%20as%20outputs%3F/near/245794512" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Adam Gemmell <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Why.20can.20fn.20ptrs.20not.20be.20used.20as.20outputs.3F.html#245794512">(Jul 13 2021 at 08:47)</a>:</h4>
<p>That explains the difference between in/out I was seeing, thank you</p>



<a name="246043469"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Why%20can%20fn%20ptrs%20not%20be%20used%20as%20outputs%3F/near/246043469" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> asquared31415 <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Why.20can.20fn.20ptrs.20not.20be.20used.20as.20outputs.3F.html#246043469">(Jul 15 2021 at 01:59)</a>:</h4>
<p>Hm that's interesting, why does the coercion not make sense for inout?  I would expect it to have the same type as if it were passed as <code>in</code> at first thought.</p>



<a name="246048048"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Why%20can%20fn%20ptrs%20not%20be%20used%20as%20outputs%3F/near/246048048" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Why.20can.20fn.20ptrs.20not.20be.20used.20as.20outputs.3F.html#246048048">(Jul 15 2021 at 03:22)</a>:</h4>
<p>Because the type of the variable is a ZST which refers to a specific function. How can you output to that?</p>



<a name="246070938"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/Why%20can%20fn%20ptrs%20not%20be%20used%20as%20outputs%3F/near/246070938" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Bramley <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/Why.20can.20fn.20ptrs.20not.20be.20used.20as.20outputs.3F.html#246070938">(Jul 15 2021 at 09:27)</a>:</h4>
<p>It wasn't clear to me at first (and I wasn't aware of the function item type until this thread) but this is consistent with simpler (non-<code>asm!</code>) writes:</p>
<div class="codehilite"><pre><span></span><code>fn a() { }
fn b() { }
...
    let mut f = a;  // Gets &quot;function item&quot; type.
    if &lt;cond&gt; { f = b };  // Error: type mismatch (like asm! &quot;inout&quot; and &quot;out&quot; parameters).
</code></pre></div>
<p>I think <code>asm!</code> is actually slightly more flexible than plain Rust here, since it can coerce to a function pointer for <code>in(reg)</code> without explicitly naming the type.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>